<?php if (isset($success_message)) : ?>
    <div class="noty_bar noty_theme_default noty_layout_topCenter noty_success" id="noty_success_1355937644582" style="cursor: pointer; left: 559.5px; display: block;">
        <div class="noty_message"><span class="noty_text">Guia creada con exito!</span></div>
    </div>
<?php endif; ?>
<div class="noty_bar noty_theme_default noty_layout_topCenter noty_error" id="message_error_formAddProducts" style="cursor: pointer; left: 559.5px;">
    <div class="noty_message"><span id="contentMessageErrorFormAddProducts" class="noty_text">El codigo ya existe para otro producto!</span></div>
</div>

<div class="grid_container">
    
        
    <?=validation_errors('<div class="errors">','</div>'); ?>
    <?php
    $attributes = array(
        'id' => 'formAddProducts',
        'class' => 'form_container'
    );
    echo form_open_multipart('products/submit', $attributes);?>
    
	<div class="grid_12">
		<div class="widget_wrap">
			<div class="widget_top">
				<span class="h_icon money"></span>
				<h6><?php $estado = isset($product->code)? 'Editar' : 'Agregar' ?> Producto</h6>
                                <input name="estado" id="estado" type="hidden" value="<?php  echo $estado;?>">
			</div>
			<div class="widget_content">
				<ul>
				<!-- <?php /*if (!isset($product->code)) : */?> -->
					<li>
						<div class="form_grid_12" >
							<div class="form_input" >
								<div class="form_grid_12 alpha"> 
									<span class="label_head" > Codigo<span class="req"  >*</span></span>
									 
								<?php


									if(isset($product->code)){
   									$data = array(
                                    	'readonly'  => 'readonly',
                                        'name'        => 'products_id',
                                        'id'          => 'code_product',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('code'), $product->code),
                                        'tabindex'    => '1',
                                        'required'
                                    );
                                    echo form_input($data);
                                }else{ ?>
                                	 <input type="text" name="code" id="code_product" class="noEmpty" value="<?php echo @field(set_value('code'), $product->code); ?>" tabindex="1" required>
                              <?php  }
                                    ?>
								</div> 
								<span class="clear"></span>
							</div>
						</div>
					</li>
				<?php /*endif; */ ?>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Descripcion <span class="req">*</span></span>
									 <input type="text" name="description" id="description" required class="noEmpty" value="<?php echo @field(set_value('description'), $product->description); ?>" tabindex="2" >

								<!-- 	<?php
                                /*    $data  /* = array(
                                        'name'        => 'description',
                                        'id'          => 'description',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('description'), $product->description),
                                        'tabindex'    => '2',
                                    );
                                    echo form_input($data);*/
                                    ?> -->
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Categoría <span class="req">*</span></span>
									<?php 
                                                                            $options = array();
                                                                            $options[-1] = "";
                                                                            foreach ($productGroups as $pg) {
                                                                                    $options[$pg->id] = $pg->description;
                                                                                }

                                                                                $extra = 'id="client" tabindex="3" style="width:350px"  required class="chzn-select full" data-placeholder="Elegir..."';


                                                                                echo form_dropdown('group_id', $options, @field(set_value('group_id'), $product->group_id), $extra);
                                                                        ?>
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
					<?php if (!isset($product->code)) : ?>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Fecha de Registro <span class="req">*</span></span>
									<?php
                                    $data = array(
                                    	'disabled'    =>'disabled',
                                        'name'        => 'insert_date',
                                        'id'          => 'insert_date',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('insert_date'), $product->insert_date, date('d/m/Y')),
                                        'tabindex'    => '4',
                                        'OnFocus'	  => 'this.blur()'
                                    );
                                    echo form_input($data);
                                    ?>
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
					<?php endif; ?>
				</ul>
			</div>
	    </div>
		<div class="widget_wrap">
			<div class="widget_top">
				<span class="h_icon list"></span>
				<h6>Unidad por mayor (empaque)</h6>
			</div>
			<div class="widget_content">
				<ul>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Descripcion(Ej: Docena,Set o Pack) <span class="req">*</span></span>
									 <input type="text" name="pack_desc" id="pack_desc" required class="noEmpty" value="<?php echo  @field(set_value('pack_desc'), $product->pack_desc); ?>" tabindex="2" >
									<!-- <?php
                                   /* $data = array(
                                        'name'        => 'pack_desc',
                                        'id'          => 'pack_desc',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('pack_desc'), $product->pack_desc),
                                        'tabindex'    => '5',
                                    );
                                    echo form_input($data);*/
                                    ?>  -->
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Unidades que contiene<span class="req">*</span></span>
									 <input type="text" name="pack_units" id="pack_units" required class="onlyNumber" value="<?php echo  @field(set_value('pack_units'), $product->pack_units); ?>" tabindex="2">
									<!-- <?php
                                   /* $data = array(
									<!-- <?php
                                   /* $data = array(
                                        'name'        => 'pack_units',
                                        'id'          => 'pack_units',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('pack_units'), $product->pack_units),
                                        'tabindex'    => '6',
                                    );
                                    echo form_input($data);
                                    */?>  -->
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">x Caja <span class="req">*</span></span>
									 <input type="text" name="box_packs" id="box_packs" required class="onlyNumber" value="<?php echo  @field(set_value('box_packs'), $product->box_packs); ?>" tabindex="2">
									<!-- <?php
                                    /*$data = array(
                                        'name'        => 'box_packs',
                                        'id'          => 'box_packs',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('box_packs'), $product->box_packs),
                                        'tabindex'    => '7',
                                    );
                                    echo form_input($data);
                                    */?> -->
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Costo del Empaque US$ <span class="req">*</span></span>
									 <input type="text" name="cost" id="cost" required class="" value="<?php echo  @field(set_value('cost'), $product->cost); ?>" tabindex="2">

									<!-- <?php
                                    /*$data = array(
                                        'name'        => 'cost',
                                        'id'          => 'cost',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('cost'), $product->cost),
                                        'tabindex'    => '8',
                                    );
                                    echo form_input($data);
                                   */ ?> -->
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Precio de  Venta del Empaque US$ <span class="req">*</span></span>
									<input type="text" name="price" id="price" required class="" value="<?php echo  @field(set_value('price'), $product->price); ?>" tabindex="2">
									<!-- <?php
                                   /* $data = array(
                                        'name'        => 'price',
                                        'id'          => 'price',
                                        'class'       => 'noEmpty',
                                        'value'       => @field(set_value('price'), $product->price),
                                        'tabindex'    => '9',
                                    );

                                   echo form_input($data);
                                    */?> -->
								</div>
								<span class="clear"></span>
							</div>
						</div>
					</li>
				</ul>
				<span class="clear"></span>
			</div>
		</div>
		<div class="widget_wrap">
			<div class="widget_top">
				<span class="h_icon list"></span>
				<h6>Foto</h6>
			</div>
			<div class="widget_content">
				<ul>
					<li>
						<div class="form_grid_12">
							<div class="form_input">
								<div class="form_grid_12 alpha">
									<span class="label_head">Ruta <span class="req"></span></span>
                                                                        <?php 
/* @var $product type */                                                   //subida de imagen                    
                                                                        if(isset($product->code))
                                                                        { ?>
									<img src="<?php  echo base_url()."images/products/".$product->picture; ?>" width="180" height="214"><br>
                                                                       <?php  } 
                                                                                                 
									
                                    $data = array(
                                     
                                        'name'        => 'path_image',
                                        'id'          => 'path_image',
                                        'tabindex'    => '10',
                                        'value'        =>  @field(set_value('path_image'), $product->picture),
                                        
                                    );
                                    echo form_upload($data);
                                    
                                    ?> 
								</div>

								<span class="clear"></span>
							</div>
						</div>
					</li>
				</ul>
				<span class="clear"></span>
			</div>
		</div>
		<br />

<script type="text/javascript">
                    $(document).ready(function() {

                        function checkCode() {

                            $.ajax({
                                'url': url + '/products/evalue',
                                'type': 'POST',
                                'data': {'products_id': $('input[name=code]').val()},
                                'success': function(res) {
                                   // alert("alert res: " + res);
                                    if (res == "true") {                                        
                                       $('div[id=noty_success_1355937644582]').css('display', 'none');
                                       $('div[id=message_error_formAddProducts]').css('display', 'block');
                                    } else if (res == "false") {       
                                       
                                       $('#formAddProducts').submit();
                                    }
                                }
                            });

                        }

                        $('#code').focus(function() {
                            $('#code').css('background', '#ffffff');
                        })
                        $('input[name=date]').focus(function() {
                            $('input[name=date]').css('background', '#ffffff');
                        })


                        function formtrue() {
                            $('formAddProducts').submit(function() {
                                return true;
                            });
                        }
                        function formfalse() {
                            $('formAddProducts').submit(function() {
                                return false;
                            });
                        }

                        $('#guardar_guia').click(function() {  
                                    if($('input[name=estado]').val()=="Editar")
                                    {
                                        $('#formAddProducts').submit();
                                    }
                                    else
                                    {
                                        checkCode(); /*funcion ajax submit*/
                                    }
                                    
                        });
                    })
                </script>
		<button type="button" id="guardar_guia" class="btn_small btn_orange"><span>Guardar</span></button> </form>
		<button type="button" class="btn_small btn_orange" onclick="javascript:window.location='<?=site_url("products"); ?>'"><span>Regresar</span></button>
	</div>
    <?php echo form_close(); ?>
	<span class="clear"></span>
</div>